#!/bin/sh

uname | grep -q MINGW && PWD_OPT="-W"
TOP_DIR="$(cd "$(dirname "$0")"/../../ && pwd $PWD_OPT)"
. "$TOP_DIR"/tools/docker/config $* >/dev/null

[ ! -d "$LAB_OUTPUT" ] && exit 0

filter=$1
[ -n "$2" ] && filter=$2

labs=`get_lab_bases "$LAB_OUTPUT" $filter`

_LAB_OUTPUT="$LAB_OUTPUT"

get_lab_attrs () # lab_attr
{
  lab_user=$1
  lab_life=$2
  lab_start=$3$4
}

for lab in $labs
do
  lab_basename=$lab
  _lab_name=$(echo $lab_basename | cut -d'+' -f1)
  lab_name=${_lab_name%-*}

  lab_attrs=$(echo $lab_basename | cut -d'+' -f2 | tr '-' ' ')
  get_lab_attrs $lab_attrs

  TOP_DIR="$_LAB_OUTPUT/$lab"
  CURRENT=$lab_name
  unset UNIX_PWD VNC_PWD VNC_PWD_VIEWONLY CONTAINER_NAME VNC_IP VNC_TOKEN
  . "$TOP_DIR/tools/docker/config" >/dev/null 2>&1

  # Ignore the containers not running
  [ -z "$CONTAINER_NAME" ] && continue

  docker ps -f name=$CONTAINER_NAME | grep -qv PORTS
  [ $? -ne 0 ] && continue

  lab_start=`get_starttime $lab_start`
  lab_end=`get_endtime "$lab_start" $lab_life`
  echo $lab_name $lab_user $HOST $HOST_VNC_PORT $VNC_TOKEN $VNC_PWD $VNC_PWD_VIEWONLY $VNC_IP \
	$HOST_WEBSSH_PORT $UNIX_PWD $CONTAINER_NAME $lab_life $lab_start $lab_end
done
